<template>
  <div class="level-pie" ref="levelPieRef"></div>
</template>

<script setup lang="ts">
import { getMessageLevelCountAPI } from '@/api/overview'
import * as echarts from 'echarts'

import { onBeforeUnmount, onMounted, ref } from 'vue'
let handleResize = () => {}
const levelPieRef = ref()
const levelPie = async () => {
  data.value = await getMessageLevelCountAPI()
  const myLevelPie = echarts.init(levelPieRef.value)
  myLevelPie.setOption({
    title: {
      text: '公告等级分布图',
      left: 'left',
      textStyle: {
        fontSize: 15
      }
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '3%',
      left: 'center'
    },
    series: [
      {
        name: '公告等级分布',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['50%', '50%'],

        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 9,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: data.value.data
      }
    ]
  })
  const handleResize = () => {
    myLevelPie.resize()
  }
  window.addEventListener('resize', handleResize)
}

const data = ref()

onMounted(() => {
  levelPie()
})
onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped></style>
